//
//  BaseTableVC.m
//  ocCrazy
//
//  Created by mac on 2018/7/3.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "BaseTableVC.h"
#import "BaseTableViewCell.h"
#import "BaseTableViewHeaderFooterView.h"
#import <objc/runtime.h>
#import "Tool.h"
@interface BaseTableVC ()<UITableViewDelegate,UITableViewDataSource,TableViewCellDelegate,BaseTableViewHeaderFooterViewDelegate>
{
    
    NSArray * _indexArr;
    NSString *_type;
}
@property (nonatomic,strong) NSArray <NSArray <NSString *> *> * classStrArr;
@property (nonatomic, strong) NSArray <NSString *> *headFootArr;

@property (nonatomic, strong) NSArray *noHeaderArr;
@property (nonatomic, strong) NSArray *noFooterArr;

@end

@implementation BaseTableVC


- (void)viewDidLoad {
    if (!_type) {
        self.classStrArr = [self.tableViewInterface classArr];
    }
    if ([self.tableViewInterface respondsToSelector:@selector(viewClassForSectionFooterHeader)]) {
        self.headFootArr = [self.tableViewInterface viewClassForSectionFooterHeader];
    }
    _page = 1;
    
    [super viewDidLoad];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    [self.tableView isGroup];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
#ifdef DEBUG
    if (!self.tableView.delegate ) {
        NSLog(@"\n 警告 \n %@ 这个类的tableView 没有设置delegate",[self class]);
    }
    if (!self.tableView.dataSource) {
        NSLog(@"\n 警告 \n %@ 这个类的tableView 没有设置dataSource",[self class]);
    }
    
    if (!self.tableViewInterface) {
         NSLog(@"\n 警告 \n %@ 这个类的tableViewInterface 没有设置\n tableViewInterface  需要设置在[super viewDidLoad]前",[self class]);
    }
#endif
}

- (void)needIndex:(NSArray *)keyArr{
    _indexArr = keyArr;
    
    [self.tableView setSectionIndexBackgroundColor:[UIColor whiteColor]];
    [self.tableView setSectionIndexColor:UIColorHex(@"#848586")];
}




- (void)setType:(NSString *)type withReload:(BOOL)reload{
    _type = type;
    self.classStrArr = [self.tableViewInterface classArrWithType:_type];
    if (reload) {
        [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:NO];
        [self.tableView reloadData];
    }
}






- (void)needRefresh{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self getData:_page];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self getData:_page];
    }];
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *class = GetClassStr(self.classStrArr,indexPath);
    
    id cell = (id)[tableView dequeueReusableCellWithIdentifier:class];
    BaseTableViewCell * baseCell = cell;
    baseCell.delegate = self;
    baseCell.model = [self getModel:indexPath];
    baseCell.indexPath = indexPath;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *class = GetClassStr(self.classStrArr, indexPath);
    Class cellClass = NSClassFromString(class);
    BaseModel *model = [self getModel:indexPath];
    return [cellClass cellHeight:model];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([self.tableViewInterface respondsToSelector:@selector(rowsCount:)]) return [self.tableViewInterface rowsCount:section];
    if (self.tableView.style == UITableViewStyleGrouped) return 1;
    return _same ? [self.classStrArr[section] count] : self.dataSouece.count;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([self.tableViewInterface respondsToSelector:@selector(sectionCount)]) return [self.tableViewInterface sectionCount];
    if (self.tableView.style == UITableViewStylePlain) return 1;
    return _same ? self.classStrArr.count : self.dataSouece.count;
 
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if ([self.tableViewInterface respondsToSelector:@selector(sectionFooterHeight:)]) {
        return [self.tableViewInterface sectionFooterHeight:section];
    }
    if ([self.noFooterArr containsObject:@(section)]) return CGFLOAT_MIN;
    if (self.headFootArr && self.headFootArr.count == 2) {
       Class Footer = NSClassFromString(self.headFootArr[1]);
        if (Footer) {
            return [Footer viewSize:nil withSection:section];
        }
    }
    return CGFLOAT_MIN;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if ([self.tableViewInterface respondsToSelector:@selector(sectionHeaderHeight:)]) {
        return [self.tableViewInterface sectionHeaderHeight:section];
    }
     if ([self.noHeaderArr containsObject:@(section)]) return CGFLOAT_MIN;
    if (self.headFootArr && self.headFootArr.count >= 1) {
        if (self.headFootArr[0] == nil || [self.headFootArr[0] isEqualToString:@""])return CGFLOAT_MIN;
        Class Header = NSClassFromString(self.headFootArr[0]);
        if (Header) {
            return [Header viewSize:nil withSection:section];
        }
    }
    return CGFLOAT_MIN;
}



-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    BaseTableViewHeaderFooterView *footer = nil;
    if ([self.noFooterArr containsObject:@(section)]) return footer;
    if (self.headFootArr && self.headFootArr.count == 2) {
        Class Footer = NSClassFromString(self.headFootArr[1]);
        if (Footer) {
            id footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headFootArr[1]];
            [footerView setSection:section];
            [footerView setDelegate:self];
            [footerView setModel:[self getHeaderFooterModel:section withType:@"2"]];
            footer = footerView;
        }
    }
    return footer;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    BaseTableViewHeaderFooterView *header = nil;
    if ([self.noHeaderArr containsObject:@(section)]) return header;
    if ( self.headFootArr && self.headFootArr.count >= 1) {
        if (self.headFootArr[0] == nil || [self.headFootArr[0] isEqualToString:@""])return header;
        Class Header = NSClassFromString(self.headFootArr[0]);
        if (Header) {
            id headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headFootArr[0]];
            [headerView setSection:section];
            [headerView setDelegate:self];
            [headerView setModel:[self getHeaderFooterModel:section withType:@"1"]];
            header = headerView;
        }
    }
    return header;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if ([self.tableViewInterface respondsToSelector:@selector(tableViewSelectAtIndexPath:)]) return [self.tableViewInterface tableViewSelectAtIndexPath:indexPath];
}


-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{

    return _indexArr;
}

-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
    
    return index >= 2 ? index - 1 : 0;
}


-(void)setClassStrArr:(NSArray<NSArray<NSString *> *> *)classStrArr{
    _classStrArr = classStrArr;
    if (_classStrArr != nil) {
        [self.tableView setCellArrForResCell:CellArr(classStrArr)];
    }
}

-(void)setHeadFootArr:(NSArray<NSString *> *)headFootArr{
    _headFootArr = headFootArr;
    if (headFootArr) {
        [self.tableView setHeaderFooterArr:headFootArr];
    }
}

-(NSArray *)noHeaderArr{
    if (!_noHeaderArr) {
        if ([self.tableViewInterface respondsToSelector:@selector(noViewHeaderSection)]) {
            _noHeaderArr = [NSArray arrayWithArray:[self.tableViewInterface noViewHeaderSection]];
        }else{
            _noHeaderArr = [NSArray array];
        }
    }
    return _noHeaderArr;
}

-(NSArray *)noFooterArr{
    if (!_noFooterArr) {
        if ([self.tableViewInterface respondsToSelector:@selector(noViewFooterSection)]) {
            _noFooterArr = [NSArray arrayWithArray:[self.tableViewInterface noViewFooterSection]];
        }else{
            _noFooterArr = [NSArray array];
        }
    }
    return _noFooterArr;
}


-(NSMutableArray *)dataSouece{
    if (!_dataSouece) {
        _dataSouece = [NSMutableArray array];
    }
    return _dataSouece;
}

- (BaseModel *)getModel:(NSIndexPath *)indexPath{
    BaseModel *model;
    if ([self.tableViewInterface respondsToSelector:@selector(modelForCell:)]){
        model = [self.tableViewInterface modelForCell:indexPath];
    }else{
        if (_dataSouece.count == 0){
            return nil;
        }else{
            if (self.tableView.style == UITableViewStylePlain){
                model = _dataSouece[indexPath.row];
            }
            
            else if (self.tableView.style == UITableViewStyleGrouped)
            {
                model = _dataSouece[indexPath.section];
            }
        }
    }
    return model;
}

- (BaseModel *)getHeaderFooterModel:(NSInteger )section withType:(NSString *)type{
    BaseModel *model;
    if ([self.tableViewInterface respondsToSelector:@selector(modelForHeaderFooter:withType:)]) {
        model = [self.tableViewInterface modelForHeaderFooter:section withType:type];
    }
    return model;
}

- (void)detailModelwithArr:(NSArray <__kindof BaseModel *> *)arr withCallBack:(void(^)(void))callBack{
    if (self.page == 1) {
        [self.dataSouece removeAllObjects];
    }
    for (__kindof BaseModel *model in arr) {
        [self.dataSouece addObject:model];
    }
    [self.tableView cc_endReFresh:arr];
    if (callBack) {
        callBack();
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
